//go:build debug

package message

import (
	"sync/atomic"
	"time"

	"gitee.com/ameise84/e3log"
)

var (
	_lastStepLogTime time.Time
	_gStepSlowCount  atomic.Int32
)

func (ts *Message) CheckStepTime(call string) {
	t := time.Now()
	d := t.Sub(ts.lastTime)
	if d > _gStepSlow {
		n := _gStepSlowCount.Add(1)
		if t.Sub(_lastStepLogTime).Seconds() > 2 {
			e3log.Msg(e3log.LevelWarn, "msg is too slow").Object(ts).Str("step", call).Int64("time cost(ms)", d.Milliseconds()).Int32("total", n).Println()
			_lastStepLogTime = t
		}
	}
	ts.step = append(ts.step, &stepRecorder{cost: t.Sub(ts.lastTime).Seconds(), call: call})
	ts.lastTime = t
}
